#include "Dataset.h"
#include "Database.h"
#include <time.h>

class TongjiDatabase : public Database
{
private:
    /* data */

protected:
    virtual int get_person_id(string filename) const
    {
        return (atoi(filename.c_str()) - 1) / 10;
        //return atoi(filename.substr(0, 4).c_str());
    }

public:
    using Database::Database;
    // TongjiDatabase(string dir) : Database(dir){};
    ~TongjiDatabase(){};

    virtual void load()
    {
        string filename;
        dirent *file;
        DIR *m_dir = opendir(dir.c_str());
        this->dir = dir;

        //std::cout << "loading database..." << std::endl;
        while ((file = readdir(m_dir)) != NULL)
        {
            if (strcmp(file->d_name, ".") == 0 ||
                strcmp(file->d_name, "..") == 0)
                continue;
            filename = file->d_name;
            int person_id = this->get_person_id(filename);
            cv::Mat img = cv::imread(dir + "/" + filename, 0);
            if (datas.find(person_id) == datas.end())
            {
                vector<palmprint> *v = new vector<palmprint>();
                datas.insert(std::pair<int, std::shared_ptr<vector<palmprint>>>(person_id, std::shared_ptr<vector<palmprint>>(v)));
            }
            std::shared_ptr<vector<palmprint>> v = datas.at(person_id);
            v->push_back(palmprint(person_id, filename, img));
        }
    };

    virtual int next_radom_person(int person_id)
    {
        int a = person_id;
        while (a == person_id)
        {
            srand((unsigned)time(NULL));
            a = rand() % this->person_num();
        }
        return a;
        // return (person_id + 10) % this->person_num();
    }

    virtual int next_person(int person_id)
    {
        return (person_id + 1) % this->person_num();
    }

    virtual int get_first_person()
    {
        return 0;
    }
};
